class User < ActiveRecord::Base

  has_and_belongs_to_many :roles

  validates_date :birth, :on_or_before => lambda { Date.current.years_ago(18) }, :on_or_before_message => I18n.t(:user_under_18)
  validates_presence_of :password_confirmation

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable


  attr_accessible :roles, :birth, :name, :email, :password, :password_confirmation, :remember_me, :role_ids

  def role?(role)
    return !!self.roles.find_by_name(role.to_s.camelize)
  end

  def password_match?
    self.password == self.password_confirmation
  end
end
